/**
 * 
 */
package org.monkeypuzzler.css;

import java.awt.Color;
import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Style implements Serializable, Cloneable
{
	private static final long serialVersionUID = -4657634673279098932L;
	
	public Color backgroundColor = null;
	public Color color = null;
	public String position = null;
	
	void update(JSONObject json) throws JSONException
	{
		//clear old values
		this.backgroundColor = null; 
		this.color = null;
		this.position = null;
		
		if (json.has("color") && json.get("color") != null)
		{
			JSONArray color = json.getJSONArray("color");
			this.color = new Color(color.getInt(0), color.getInt(1), color.getInt(2));
		}
		
		if (json.has("backgroundColor") && json.get("backgroundColor") != null)
		{
			JSONArray color = json.getJSONArray("backgroundColor");
			this.backgroundColor = new Color(color.getInt(0), color.getInt(1), color.getInt(2));
		}
		
		this.position = json.getString("position");
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((backgroundColor == null) ? 0 : backgroundColor
						.hashCode());
		result = prime * result + ((color == null) ? 0 : color.hashCode());
		result = prime * result
				+ ((position == null) ? 0 : position.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (this.getClass() != obj.getClass())
			return false;
		final Style other = (Style) obj;
		if (backgroundColor == null) {
			if (other.backgroundColor != null)
				return false;
		} else if (!backgroundColor.equals(other.backgroundColor))
			return false;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (position == null) {
			if (other.position != null)
				return false;
		} else if (!position.equals(other.position))
			return false;
		return true;
	}
	
	@Override
	protected Style clone() 
	{
		try
		{
			return (Style) super.clone();
		}
		catch (CloneNotSupportedException e)
		{
			return null;
		}
	}
	
}